﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;

namespace BeerScanatronUI
{
    public partial class UserImage : UserControl, IUserImage
    {
        /// <summary>
        /// Allowed overlay images.
        /// </summary>
        public enum Effects
        {
            None,
            Allow,
            DoNotAllow
        }

        // Backing store.
        private Image _user;
        private Effects _effects;
        private string _appResourcePath;

        /// <summary>
        /// Constructor: Initialize user and overlay.
        /// </summary>
        public UserImage()
        {
            _user = null;
            _effects = Effects.None;
            _appResourcePath = "BeerScanatronUI.Resources.";

            InitializeComponent();
        }

        #region IUserImage Impl

        /// <summary>
        /// Set image then repaint.
        /// </summary>
        /// <param name="userImage"></param>
        void IUserImage.SetUserImage(Image userImage)
        {
            _user = userImage;
            this.Invalidate();
        }

        /// <summary>
        /// Set image then repaint.
        /// </summary>
        /// <param name="userImage"></param>
        void IUserImage.GetUserImageFromAppResources(string imageName)
        {
            string resourcePath = _appResourcePath + imageName;
            Bitmap image = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath));
            ((IUserImage)this).SetUserImage(image);
        }

        /// <summary>
        /// Get user image.
        /// </summary>
        /// <returns></returns>
        Image IUserImage.GetUserImage()
        {
            return _user;
        }

        /// <summary>
        /// Visual effects for image.
        /// </summary>
        /// <param name="color"></param>
        void IUserImage.SetImageEffects(Effects effect)
        {
            _effects = effect;

            switch (effect)
            {
                case Effects.None:
                    this.BackColor = Consts.Theme.InitialUserImageBackColor;
                    break;
                case Effects.Allow:
                    this.BackColor = Color.Green;
                    break;
                case Effects.DoNotAllow:
                    this.BackColor = Color.Red;
                    break;
            }
        }

        /// <summary>
        /// Visual effects for image.
        /// </summary>
        /// <param name="color"></param>
        Effects IUserImage.GetImageEffects()
        {
            return _effects;
        }

        #endregion

        /// <summary>
        /// Paint control.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserImage_Paint(object sender, PaintEventArgs e)
        {
            // Fill in backgound with white.
            Brush brush = new SolidBrush(this.BackColor);
            e.Graphics.FillRectangle(brush, this.ClientRectangle);

            // Calc image rectangle so there is always a thin white border around picture.
            int border = 6;
            Rectangle imageRect = new Rectangle(
                this.ClientRectangle.X + border, this.ClientRectangle.Y + border,
                this.ClientRectangle.Width - 2 * border, this.ClientRectangle.Height - 2 * border);

            //e.Graphics.DrawRectangle(Pens.Brown, imageRect);

            if (_user != null)
            {
                e.Graphics.DrawImage(_user, imageRect);
            }
        }
    }
}
